*****************************************************************************
*			          Figures Descriptives with ISSP	       				*
*																			*			
* Author: 			Valentina Gonzalez Rostani		        				*
* Contact: 			mag384@pitt.edu              							*
* Date: 			August 9 2024   										*
* Version:			Stata 17 												*                                                                          
*																			*
*****************************************************************************
/*
This do-file:
- Creates Figure 1, 2 and A3 using data from ISSP. 

Input:
- Data\Figures_ISSP.dta

Output:
- Figure 1: Relative Share of Labor Force 1995 to 2014 [Figures\Relative Share of Labor Force 1998 to 2014 ISSP.pdf]
- Figure 2: Electoral consequences, Routine and Non-Routine Voters [Figure/price_by_mpg.pdf]
- Figure A3: Importance of job security, Difficulties to find a new job, Concerns about losing the job and Job dissatisfaction [Figure/jobdisatisfactionpredictedtogetherall.pdf]
*/

*Defining Directory
cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication"

*Calling the data
use "Data\Figures_ISSP.dta", clear 

*******************************************************************************
* Graphs
*******************************************************************************
* Graph style for F1 & 2
{
grstyle clear
set scheme s2color
grstyle init
grstyle set plain, box
grstyle color background white
grstyle color major_grid gs8
grstyle linepattern major_grid dot
}
// Figure 1: Relative Share of Labor Force 1995 to 2014
{
    * Generating summary statistics with three task categories:
    * Task 1, Task 2, and Task 33 following Autor (2003) and coded by Kurer and Gallego (2019)
    
    * Preserve the current dataset in memory so it can be restored later
    preserve 
    
    * Collapse the data to calculate the mean of task1, task2, and task33 weighted by 'weight' for each year
    collapse task1 task2 task33 [aw=weight], by(year)
    
    * Keep only the observations where the year is greater than 1997 to look post automation shock
    keep if year > 1997
    
    * Create a line graph for the task categories over time
    * The first line represents Non-Routine Cognitive tasks (green line)
 graph twoway line  task1 year if year>1997, lc(green) legend(label(1 "Non-Routine Cognitive")) || ///
  line  task2 year if year>1997, lc(red) legend(label(2 "Routine")) || ///
    line  task33 year if year>1997, lc(blue) legend(label(3 "Non-Routine Manual")) 

    
    * Export the graph as a PDF file with the specified name, replacing any existing file
    graph export "Figure\Relative Share of Labor Force 1998 to 2014 ISSP.pdf", as(pdf) replace
    
    * Restore the original dataset that was in memory before the collapse
    restore
}

// Figure 2: Electoral consequences, Routine and Non-Routine Voters
{
* Here I look at the share of votes by party family, and instead of using the three categories of risks, I put together non-routine manual and routine. I repeat this for RR, ML, MR, Non-voters. Code commented in the first one. 
// Radical right (graph commented)
{
* Label the variable 'radicalR' to represent "Votes for Right Populist (%)"
lab var radicalR "Votes for Right Populist (%)"

* Preserve the current dataset in memory so it can be restored later
preserve 	

* Generate a new variable 'task2and3' initialized to 1 if 'task2' equals 1
gen task2and3 = 1 if task2 == 1

* Replace 'task2and3' with 1 if 'task33' equals 1 (combining task2 and task33)
replace task2and3 = 1 if task33 == 1

* Replace 'task2and3' with 0 if 'task1' equals 1 (Non-Routine Cognitive tasks)
replace task2and3 = 0 if task1 == 1

* Keep only observations where 'emplB' is less than 6 (likely filtering by employment status or category)
keep if emplB < 6

* Collapse the data to calculate the mean of 'radicalR' weighted by 'weight' for each year, task1, and task2and3 categories
collapse radicalR [aw=weight], by(year task1 task2and3)

* Create a line graph for 'radicalR' over time, segmented by task categories
graph twoway line  radicalR year if task1==1, lc(green) legend(label(1 "Non-Routine Cognitive"))  || ///
  line  radicalR year if task2and3==1, lc(red) legend(label(2 "Routine & Manual")) ytitle("Votes for Right Populist (%)", size(small))    legend(size(vsmall))  xtitle("Year", size(small))   ylabel(, labsize(vsmall))  xlabel(, labsize(vsmall))

* Save the graph as "RR.gph" in the "Figure" directory, replacing any existing file with the same name
graph save "Figure/RR.gph", replace

* Restore the original dataset that was in memory before the modifications
restore
}
// Maintream left
{
 preserve 	

gen task2and3=1 if task2==1
replace task2and3=1 if task33==1
replace task2and3=0 if task1==1

keep if emplB<6
collapse mainstreamleft [aw=weight], by(year task1 task2and3)

graph twoway line  mainstreamleft year if task1==1, lc(green)   legend(label(1 "Non-Routine Cognitive"))  || ///
  line  mainstreamleft year if task2and3==1, lc(red) legend(label(2 "Routine & Manual"))  ytitle("Votes for Mainstream Left (%)", size(small))    legend(size(vsmall))  xtitle("Year", size(small))   ylabel(, labsize(vsmall))  xlabel(, labsize(vsmall))
graph save "Figure/ML.gph", replace

restore	
}
// Mainstream right
{
 preserve 	

gen task2and3=1 if task2==1
replace task2and3=1 if task33==1
replace task2and3=0 if task1==1

keep if emplB<6
collapse mainstreamright [aw=weight], by(year task1 task2and3)

graph twoway line  mainstreamright year if task1==1, lc(green)  legend(label(1 "Non-Routine Cognitive"))  || ///
  line  mainstreamright year if task2and3==1, lc(red) legend(label(2 "Routine & Manual"))  ytitle("Votes for Mainstream Right (%)", size(small))    legend(size(vsmall))  xtitle("Year", size(small))   ylabel(, labsize(vsmall))  xlabel(, labsize(vsmall))
graph save "Figure/MR.gph", replace

restore	
}
// Non Voters
{
 preserve 	

gen task2and3=1 if task2==1
replace task2and3=1 if task33==1
replace task2and3=0 if task1==1

keep if emplB<6
collapse nonvoters [aw=weight], by(year task1 task2and3)

graph twoway line  nonvoters year if task1==1, lc(green)  legend(label(1 "Non-Routine Cognitive"))  || ///
  line  nonvoters year if task2and3==1, lc(red) legend(label(2 "Routine & Manual"))   ytitle("Non-Voters (%)", size(small))    legend(size(vsmall))  xtitle("Year", size(small))   ylabel(, labsize(vsmall))  xlabel(, labsize(vsmall))
graph save "Figure/Nv.gph", replace

restore	
}
 * Combine multiple graphs into a single figure
graph combine "Figure/ML.gph" "Figure/MR.gph" "Figure/RR.gph" "Figure/Nv.gph"
* Export the combined graph as a PDF file named "price_by_mpg.pdf" in the "Figure" directory
graph export "Figure/price_by_mpg.pdf", as(pdf) replace

* Erase (delete) the individual graph files from the "Figure" directory after combining them
erase "Figure/ML.gph"
erase "Figure/MR.gph"
erase "Figure/RR.gph"
erase "Figure/Nv.gph"



}
// Figure A3: Importance of job security, Difficulties to find a new job, Concerns about losing the job and Job dissatisfaction
{
* The code categorizes individuals based on whether they find it difficult to get a new job, then runs a logistic regression to assess how the risk of automation influences this difficulty, adjusting for weights. It calculates the predicted probabilities of job difficulty across different levels of automation risk and generates a graph to visualize these probabilities, saving the result as a file.
* Code commented for first graph whether it is difficult to find a new job. Then code for the other three is similar (levels of variables changes so the dummies are different). 


* Graph style for figure A3
{
grstyle clear
set scheme s2color
grstyle init
grstyle set plain, nogrid
grstyle color background white
}
* W_easynewjob
{
* Generate a new variable 'W_easynewjob_difficult' to categorize the difficulty of finding a new job
* Set 'W_easynewjob_difficult' to 1 if 'W_easynewjob' is 4 or 5 (indicating it is difficult)
gen W_easynewjob_difficult = 1 if W_easynewjob == 4 | W_easynewjob == 5
* Replace 'W_easynewjob_difficult' with 0 if 'W_easynewjob' is 1, 2, or 3 (indicating it is not difficult)
replace W_easynewjob_difficult = 0 if W_easynewjob == 1 | W_easynewjob == 2 | W_easynewjob == 3

* Run a logistic regression model with 'W_easynewjob_difficult' as the dependent variable
logit W_easynewjob_difficult rti [pweight=weight]

* Calculate the marginal effects of 'rti' on the predicted probability of 'W_easynewjob_difficult'
* atmeans calculates the marginal effect at the means of the covariates
* at() specifies a range of values for 'rti' from -1.52 to 2.24 with increments of 0.05
margins, atmeans at(rti=(-1.52(0.05)2.24))

* Plot the margins with a line graph and display the confidence intervals as dotted lines
 margins, atmeans at(rti=(-1.52(0.05)2.24)) 
     marginsplot , recast(line) recastci(rline) ci1opts(fintensity(50) lpattern(dot)) xti(Risk of automation) yti("Predicted probability (95% CI)") ti("Job dissatisfaction")  saving("Figure/difficult.gph", replace)
}

	 
* W_satisfaction
{
gen dissatisfied=1 if W_satisfaction==5 | W_satisfaction==6 | W_satisfaction==7
replace dissatisfied=0 if W_satisfaction==1 | W_satisfaction==2 | W_satisfaction==3 | W_satisfaction==4


logit dissatisfied rti [pweight=weight]

 margins, atmeans at(rti=(-1.52(0.05)2.24)) 
     marginsplot , recast(line) recastci(rline) ci1opts(fintensity(50) lpattern(dot)) xti(Risk of automation) yti("Predicted probability (95% CI)") ti("Job dissatisfaction")  saving("Figure/dissatisfied.gph", replace)
}	 

	 
* W_losing
{
gen W_losing2=1 if W_losing==1 | W_losing==2 | W_losing==3
replace W_losing2=0 if W_losing==4 


logit W_losing2   rti [pweight=weight]

 margins, atmeans at(rti=(-1.52(0.05)2.24)) 
     marginsplot , recast(line) recastci(rline) ci1opts(fintensity(50) lpattern(dot)) xti(Risk of automation) yti("Predicted probability (95% CI)") ti("Concerns about losing job") saving("Figure/losing.gph", replace)
}

* W_jobsec
{
gen W_jobsec2=1 if W_jobsec==1 | W_jobsec==2
replace W_jobsec2=0 if W_jobsec==3 | W_jobsec==4 | W_jobsec==5


logit W_jobsec2 rti [pweight=weight] 
 margins, atmeans at(rti=(-1.52(0.05)2.24)) 
     marginsplot , recast(line) recastci(rline) ci1opts(fintensity(50) lpattern(dot)) xti(Risk of automation) yti("Predicted probability (95% CI)") ti("Importance of job security") saving("Figure/security.gph", replace)
}
* Combine the specified graphs into one figure
graph combine "Figure/security.gph" "Figure/difficult.gph" "Figure/losing.gph" "Figure/dissatisfied.gph"

* Export the combined graph as a PDF file, replacing any existing file
graph export "Figure/jobdisatisfactionpredictedtogetherall.pdf", as(pdf) replace

* Delete the individual graph files after combining them
erase "Figure/security.gph"
erase "Figure/difficult.gph"
erase "Figure/losing.gph"
erase "Figure/dissatisfied.gph"

}